<template>
	<fui-form>

		<custom-form-item-card>
			<fui-form-item label="处理内容" label-width="auto" asterisk :bottom-border="false">
				<template #vertical>
					<fui-textarea v-model="formData.remark" :borderTop="false" placeholder="请输入"></fui-textarea>
				</template>
			</fui-form-item>
			<fui-form-item label="附件" label-width="auto" :bottom-border="false">
				<template #right>
					<custom-button-upload @change="onChange"></custom-button-upload>
				</template>
			</fui-form-item>
			<custom-file-list :list="fileList" @del="onDelFile"></custom-file-list>
		</custom-form-item-card>
	</fui-form>
</template>

<script>
	import {
		defineComponent
	} from "vue"
	import {
		WorkOrderMyIndexApi
	} from "../../../../api/workOrder";
	import {
		useHandleWorkOrder_Handle
	} from "../../../../store/modules/handleWorkOrder_Handle"
	import {
		mapWritableState
	} from 'pinia'
	import form from '@/components/firstui/fui-validator/fui-validator'
	import _ from 'lodash'
	import {
		base64ToFile
	} from "../../../../utils/util";
	import {
		FileApi
	} from "../../../../api/file";
	export default defineComponent({
		data() {
			return {
				fileList: []
			}
		},
		computed: {
			...mapWritableState(useHandleWorkOrder_Handle, ['formData'])
		},
		emits: ['disabled'],
		methods: {
			onChange(f) {
				console.log(f, 'ffff');
				this.fileList = [...this.fileList, ...f]
			},
			onDelFile(index) {
				this.fileList.splice(index, 1)
			},
			async submit(orderNum, cb) {
				const rules = [{
					name: "remark",
					rule: ["required"],
					msg: ["请输入处理内容"]
				}];
				try {
					this.formData.orderNum = orderNum
					const the = this
					let {
						isPassed,
						errorMsg
					} = form.validator(this.formData, rules)
					if (isPassed) {
						this.$emit('disabled', true)
						for (let i of this.fileList) {
							let f = base64ToFile(i.base64)
							const result = await FileApi.upload('image', f)
							const r = await FileApi.uploadWechatFile(result.mediaId)
							this.formData.processEnclosure.push(r.data)

						}
						await WorkOrderMyIndexApi.handleWorkOrder(this.formData)
						this.fui.toast('处理完成')
						_.delay(() => {
							the.formData = {
								orderNum: '',
								remark: '',
								processEnclosure: []
							}

							cb && cb()
						}, 1000)

					} else {
						this.$emit('disabled', false)
						this.fui.toast(errorMsg)
					}
				} catch (err) {
					console.error(err);
					this.$emit('disabled', false)
					this.fui.toast(err)

				}
			}
		},
	})
</script>

<style>
</style>